package evocarcassonne.tileseteditor.managers;

import evocarcassonne.tileseteditor.ui.swing.ConnectionsManager;
import evocarcassonne.core.board.Tile;
import evocarcassonne.core.utils.ZoneType;
import evocarcassonne.tileseteditor.ui.swing.MainForm;
import evocarcassonne.tileseteditor.ui.swing.components.LinksTable;
import evocarcassonne.tileseteditor.ui.swing.components.ZoneTypeComboBox;
import evocarcassonne.tileseteditor.ui.swing.TileDrawer;
import evocarcassonne.tileseteditor.utils.ZoneTypeItem;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.border.TitledBorder;

public class MainManager implements ActionListener {

    MainForm main;
    Timer timer;
    TileDrawer tileDrawer;
    LinksTable table;
    private ZoneTypeComboBox cb_north;
    private ZoneTypeComboBox cb_east;
    private ZoneTypeComboBox cb_south;
    private ZoneTypeComboBox cb_west;
    private ZoneTypeComboBox[] cbs;

    public MainManager(final MainForm main, TileDrawer tileDrawer, LinksTable table) {

        this.main = main;
        this.tileDrawer = tileDrawer;
        this.table = table;

        cb_north = (ZoneTypeComboBox) main.cb_north;
        cb_east = (ZoneTypeComboBox) main.cb_east;
        cb_south = (ZoneTypeComboBox) main.cb_south;
        cb_west = (ZoneTypeComboBox) main.cb_west;

        // comboboxes array
        cbs = new ZoneTypeComboBox[4];
        cbs[0] = cb_north;
        cbs[1] = cb_east;
        cbs[2] = cb_south;
        cbs[3] = cb_west;

        // init comboxes
        for (int iter = 0; iter < cbs.length; iter++) {
            final ZoneTypeComboBox cb = cbs[iter];

            cb.addItem(new ZoneTypeItem(ZoneType.MEADOW, "Champ"));
            cb.addItem(new ZoneTypeItem(ZoneType.CITY, "Ville"));
            cb.addItem(new ZoneTypeItem(ZoneType.ROAD, "Chemin"));

            cb.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {

                    if (cb.userAction) {
                        cb.resetFields();
                    }

                    cb.userAction = true;

                    ZoneType value = ((ZoneTypeItem) cb.getSelectedItem()).getValue();

                    if (value == ZoneType.ROAD) {
                        TileManager.setType(cb.card, ZoneType.ROAD);
                    } else if (value == ZoneType.CITY) {
                        TileManager.setType(cb.card, ZoneType.CITY);
                    } else if (value == ZoneType.MEADOW) {
                        TileManager.setType(cb.card, ZoneType.MEADOW);
                    }

                    main.showCardPanel(cb.card);

                }
            });
        }

        timer = new Timer(100, this);
        timer.setRepeats(true);
        timer.start();
    }

    public void resetAll() {

        cb_north.setIndex(0);
        cb_east.setIndex(0);
        cb_south.setIndex(0);
        cb_west.setIndex(0);

        ConnectionsManager.reset();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        refresh();
    }

    public void addTile() {
        ConnectionsManager.addMapping();
        resetAll();
        setTileParameters();
    }

    public void setTileParameters() {

        Tile tile = TileManager.getTile();

        String title = "Tile " + tile.number;
        ((TitledBorder) main.paramsTile.getBorder()).setTitle(title);
        main.paramsTile.repaint();

        // update combobox
        for (int iter = 0; iter < cbs.length; iter++) {

            ZoneTypeComboBox cb = cbs[iter];

            if (tile.getZone(cb.card).getType() == ZoneType.MEADOW) {
                cb.setIndex(0);
            } else if (tile.getZone(cb.card).getType() == ZoneType.CITY) {
                cb.setIndex(1);
            } else {
                cb.setIndex(2);
            }
        }

        // update checkbox
        ConnectionsManager.setTileConnections();
    }

    private void refresh() {
        // tile draw
        tileDrawer.repaint();

        // ComboBox
        ZoneType typeNorth = ((ZoneTypeItem) cb_north.getSelectedItem()).getValue();
        ZoneType typeEast = ((ZoneTypeItem) cb_east.getSelectedItem()).getValue();
        ZoneType typeSouth = ((ZoneTypeItem) cb_south.getSelectedItem()).getValue();
        ZoneType typeWest = ((ZoneTypeItem) cb_west.getSelectedItem()).getValue();

        // COnnections
        main.clearConns();

        if (typeNorth == typeEast) {
            main.addConn(0);
        }

        if (typeNorth == typeSouth) {
            main.addConn(1);
        }

        if (typeNorth == typeWest) {
            main.addConn(2);
        }

        if (typeSouth == typeEast) {
            main.addConn(3);
        }

        if (typeWest == typeSouth) {
            main.addConn(4);
        }

        if (typeWest == typeEast) {
            main.addConn(5);
        }

        if (typeNorth == typeEast && typeNorth == typeWest) {
            main.addConn(6);
        }

        if (typeNorth == typeEast && typeNorth == typeSouth) {
            main.addConn(7);
        }

        if (typeNorth == typeSouth && typeNorth == typeWest) {
            main.addConn(8);
        }

        if (typeWest == typeEast && typeSouth == typeWest) {
            main.addConn(9);
        }

        if (typeNorth == typeEast && typeNorth == typeWest && typeNorth == typeSouth) {
            main.addConn(10);
        }

        //links
        table.refresh();
    }
}
